﻿using BUSINESS;
using DataAccessObject;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace GUI.GiaoVien
{
    /// <summary>
    /// Interaction logic for ChuyenMonGUI.xaml
    /// </summary>
    public partial class GUIChuyenMon : Window
    {
        private List<GIAOVIEN> listGiaoVien;
        private List<GIAOVIEN> listCurrentGiaoVien = new List<GIAOVIEN>();
        private List<CHITIETMONHOC> listChiTietMonHoc = new List<CHITIETMONHOC>();
        private string filter = string.Empty;

        public GUIChuyenMon()
        {
            InitializeComponent();
            /////////////////////
            this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            InitControl();
        }

        private void InitControl()
        {
            InitListGiaoVien();
            InitListKhoiLop();
            InitListMonHoc();
        }

        private void InitListMonHoc()
        {
            txtInputMonHoc.ItemsSource = MonHocBUS.getAllMonHoc();
            if (txtInputMonHoc.Items.Count > 0) txtInputMonHoc.SelectedIndex = 0;
        }

        private void InitListKhoiLop()
        {
            txtInputKhoiLop.ItemsSource = KhoiLopBUS.getAllKhoiLop();
            if (txtInputKhoiLop.Items.Count > 0) txtInputKhoiLop.SelectedIndex = 0;
        }

        private void InitListGiaoVien()
        {
            tbGiaoVienChuyenMon.ItemsSource = listChiTietMonHoc;
            ///////////////////////////////////////////
            listGiaoVien = GiaoVienBUS.getAllGiaoVien();
            listCurrentGiaoVien.AddRange(listGiaoVien);
            ///////////////////////////////////////////
            txtListGiaoVien.ItemsSource = listCurrentGiaoVien;
            if (txtListGiaoVien.Items.Count > 0) txtListGiaoVien.SelectedIndex = 0;
        }

        public string Filter
        {
            get
            {
                return this.filter.ToUpperInvariant();
            }
            set
            {
                filter = value;
                var query = from data in listGiaoVien
                            where data.MaGiaoVien.ToUpperInvariant().Contains(Filter) ||
                            data.HoTen.ToUpperInvariant().Contains(Filter)
                            select data;
                listCurrentGiaoVien.Clear();
                listCurrentGiaoVien.AddRange(query.ToList());
                txtListGiaoVien.Items.Refresh();
            }
        }

        private void txtListGiaoVien_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            GIAOVIEN giaovien = txtListGiaoVien.SelectedItem as GIAOVIEN;
            InputGB.DataContext = giaovien;
            listChiTietMonHoc.Clear();
            listChiTietMonHoc.AddRange(GiaoVienBUS.getChuyenMonByGiaoVien(giaovien));
            tbGiaoVienChuyenMon.Items.Refresh();
            stateUnselect();
        }

        private void txtInputKhoiLop_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            KHOILOP khoilop = txtInputKhoiLop.SelectedItem as KHOILOP;
            txtInputMonHoc.ItemsSource = KhoiLopBUS.getAllMonHocByKhoiLop(khoilop);
            txtInputMonHoc.Items.Refresh();
            if (txtInputMonHoc.Items.Count > 0) txtInputMonHoc.SelectedIndex = 0;
        }

        private void btnInputLuuLai_Click(object sender, RoutedEventArgs e)
        {
            GIAOVIEN giaovien = txtListGiaoVien.SelectedItem as GIAOVIEN;
            KHOILOP khoilop = txtInputKhoiLop.SelectedItem as KHOILOP;
            MONHOC monhoc = txtInputMonHoc.SelectedItem as MONHOC;

            GiaoVienBUS.updateGiaoVienChuyenMon(giaovien, khoilop, monhoc);

            listChiTietMonHoc.Clear();
            listChiTietMonHoc.AddRange(GiaoVienBUS.getChuyenMonByGiaoVien(giaovien));
            tbGiaoVienChuyenMon.Items.Refresh();
        }

        private void tbGiaoVienChuyenMon_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            CHITIETMONHOC chitietmonhoc = tbGiaoVienChuyenMon.SelectedItem as CHITIETMONHOC;
            if (chitietmonhoc == null) return;
            stateSelected();
        }

        private void btnInputXoa_Click(object sender, RoutedEventArgs e)
        {
            CHITIETMONHOC chitietmonhoc = tbGiaoVienChuyenMon.SelectedItem as CHITIETMONHOC;
            if (chitietmonhoc == null) return;
            listChiTietMonHoc.Remove(chitietmonhoc);
            tbGiaoVienChuyenMon.Items.Refresh();
            ////////////////////////
            GIAOVIEN giaovien = txtListGiaoVien.SelectedItem as GIAOVIEN;
            if (giaovien == null) return;
            GiaoVienBUS.deleteGiaoVienChuyenMon(giaovien, chitietmonhoc);
        }

        private void stateSelected()
        {
            btnInputXoa.IsEnabled = true;
        }

        private void stateUnselect()
        {
            btnInputXoa.IsEnabled = false;
        }
    }
}
